#!/usr/bin/bash

dir=$1
cmd=$2

if [ ! $dir ];then
    echo "ERROR: 需要一个dir参数" 
    exit 1
fi
if [ ! $cmd ];then
    echo "ERROR: 需要一个cmd参数" 
    exit 1
fi

current_path=$(pwd)
if [ ! -e $current_path/$dir.tgz ];then
    echo "INFO: 解压文件"

    pos1=`grep -a -n "exit 0" $0 | sort -r | head -n 1 | cut -d : -f 1`
    pos2=`expr $pos1 + 1`

    tail -n +${pos2} $0 > $current_path/$dir.tgz
    tar zxf $current_path/$dir.tgz
    chmod +x $dir/$cmd
    echo "INFO: 文件已解压到 $current_path/$dir"
fi

echo "INFO: 执行$dir"
cd $current_path/$dir && ./$cmd
echo "INFO: 执行成功"
exit 0
